<template>
<view>
<!-- <view class="search" wx:if="{{ search_btn }}">
	<view bindtap="openSearch">
		<icon type="search" size="13"></icon>
		<text>搜索</text>
	</view>
</view> -->
<view class="search_input" v-if="search_friend">
	<view>
		<icon type="search" size="13"></icon>
		<input placeholder="搜索" placeholder-style="color:#CFCFCF;line-height:20px;font-size:12px;" auto-focus>
	</view>
	<text @tap="cancel">取消</text>
</view>
<view class="contain">
<!-- 	<view class="build_group" @tap="build_group">
		<image src="/static/images/chat-list.png"></image>
		<text>新建群聊</text>
	</view> -->
<!-- 	<view class="add_group">
		<image src="../../static/images/cell_groups.png"></image>
		<text>添加公开群</text>
	</view> -->
	<!-- <view class="line"></view> -->
	<!-- 群聊列表 -->
<!-- 	<view class="title">
		我的群聊
	</view> -->
	<view class="nbr_header"></view>
	<view v-for="(item, groupid) in myGroups" :key="groupid" class="groupList">
		<view class="info">
			<image :src="item.avatar" :data-username="item.groupname"></image>
		</view>
		<view class="nbr_body" :data-username="item.groupname" :data-qid="item.id" :data-roomid="item.groupid" @tap="into_room">
			<text :data-username="item.groupname">{{ item.groupname }}</text>
		</view>
		
		<!-- <view class="edit" @tap="edit_group" :data-username="item.groupname" :data-roomid="item.groupid">
			<image src="/static/images/cell_groups.png" :data-username="item.groupname"></image>
		</view> -->
	</view>
<!-- 	<view class="title">
		公开群
	</view>
	<view class="nbr_header"></view>
	<view v-for="(items, groupids) in PublicGroups" :key="groupids" class="groupList" @click="isinGroup(items)">
		<view class="info">
			<image :src="items.avatar" :data-username="items.groupname"></image>
		</view>
		<view class="nbr_body" :data-username="items.groupname" :data-qid="items.id" :data-roomid="items.groupid" @tap="into_room">
			<text :data-username="items.groupname">{{ items.groupname }}</text>
		</view>

	</view> -->
	
</view>

<view class="mask" @tap="close_mask" v-if="show_mask"></view>
</view>
</template>

<script>
var WebIM = require("../../utils/WebIM")["default"];
let disp = require("../../utils/broadcast");

export default {
  data() {
    return {
      search_btn: true,
      search_friend: false,
      show_mask: false,
      groupList: [],
	  GroupList:[],
	  myGroups:[],
		PublicGroups:[],
		 // 群聊列表
      myName: ""
    };
  },

  components: {},
  props: {},
  onLoad: function (option) {
    let me = this;
    disp.on("em.invite.joingroup", function () {
      var pageStack = getCurrentPages(); // 判断是否当前路由是本页

      if (pageStack[pageStack.length - 1].route === me.__route__) {
        me.GetmyGroups();
      }
    });
    disp.on("em.invite.deleteGroup", function () {
      var pageStack = getCurrentPages(); // 判断是否当前路由是本页

      if (pageStack[pageStack.length - 1].route === me.__route__) {
        me.GetmyGroups();
      }
    });
    this.setData({
      myName: option.myName
    });
	this.GetmyGroups();
  },
  onShow: function () {
 this.GetmyGroups();
  },
  methods: {
	  //判断是否在群
	  isinGroup(msg){
		  
	  },
	  GetmyGroups(){
		  uni.showLoading({
		  	title: '加载中'
		  });
		  let token = uni.getStorageSync('token')
		  uni.request({
		  	url: this.G_base + '/api/lz/qlist',
		  	method: 'POST',
		  	header: {
		  		'content-type': 'application/x-www-form-urlencoded',
		  		'Authori-zation': 'Bearer ' + token
		  	},
		  	data: {
		  		account:this.myName
		  	},
		  	success: res => {
		  		console.log(res)
				this.myGroups = res.data.data.zi
				this.PublicGroups = res.data.data.sy
				uni.hideLoading();
		  	}
		  })
	  },
    // 列出所有群组 (调用 listRooms 函数获取当前登录用户加入的群组列表)
   //  listGroups() {
   //    var me = this;
	  // uni.showLoading({
	  // 	title: '加载中'
	  // });
   //    WebIM.conn.getGroup({
   //      limit: 100,
   //      success: function (res) {
   //        me.setData({
   //          groupList: res.data
   //        }); // 好像也没有别的官方通道共享数据啊
			// me.groupList = res.data
			// for(let i =0;i<me.groupList.length;i++){
			// 	let token = uni.getStorageSync('token')
			// 	uni.request({
			// 		url: me.G_base + '/api/lz/ltzh',
			// 		method: 'POST',
			// 		header: {
			// 			'content-type': 'application/x-www-form-urlencoded',
			// 			'Authori-zation': 'Bearer ' + token
			// 		},
			// 		data: {
			// 			list: me.groupList[i].groupname,
			// 			status:2
			// 		},
			// 		processData: true,
			// 		success: res => {
			// 			me.groupList[i].headimg =res.data.data.useravatar
			// 			me.groupList =me.groupList
			// 		}
			// 	})
			// 	console.log(me.groupList)
			// 	setTimeout(()=>{
			// 		me.GroupList = me.groupList
			// 		uni.hideLoading();
			// 	},1000)
			// }
   //        getApp().globalData.groupList = res.data || [];
   //      },
   //      error: function () {}
   //    });
   //  },

    openSearch: function () {
      this.setData({
        search_btn: false,
        search_friend: true,
        show_mask: true
      });
    },
    cancel: function () {
      this.setData({
        search_btn: true,
        search_friend: false,
        show_mask: false
      });
    },
    close_mask: function () {
      this.setData({
        search_btn: true,
        search_friend: false,
        show_mask: false
      });
    },
    into_room: function (event) {
		let that = this
		let token = uni.getStorageSync('token')
		uni.request({
			url: that.G_base + '/api/lz/yhqpd',
			method: 'POST',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'Authori-zation': 'Bearer ' + token
			},
			data: {
						id:event.currentTarget.dataset.roomid,
						account:that.myName
			},
			success: res => {
				console.log(res)
				if (res.data.status == 200) {
					var nameList = {
					  myName: that.myName,
					  your: event.currentTarget.dataset.username,
					  groupId: event.currentTarget.dataset.roomid,
							qid:event.currentTarget.dataset.qid
					};
					uni.navigateTo({
					  url: "../groupChatRoom/groupChatRoom?username=" + JSON.stringify(nameList)
					});
				} else {
					uni.showModal({
						title: '提示',
						content: res.data.msg,
						success: function(res) {
							if (res.confirm) {
								that.open(event.currentTarget.dataset.roomid)
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			}
		})
      
    },
	open(msg){
		let username = uni.getStorageSync("myUsername")
		let options = {
		    groupId: msg                          // 群组ID
		};
		WebIM.conn.joinGroup(options).then((res) => {
		    console.log("环信",res)
			uni.request({
				url: this.G_base + '/api/lz/usersqq',
				method: 'POST',
				data: {
					qid: msg,
					squser: username
			
				},
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'Authori-zation': 'Bearer ' + this.token
				},
				success: res => {
					if (res.data.status == 200) {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					}
					console.log(res)
				}
			})
		})
		
		
	},
    build_group: function () {
      var me = this;
      var nameList = {
        myName: me.myName
      };
      uni.navigateTo({
        url: "../add_groups/add_groups?owner=" + JSON.stringify(nameList)
      });
    },
    edit_group: function (event) {
      var nameList = {
        myName: this.myName,
        groupName: event.currentTarget.dataset.username,
        roomId: event.currentTarget.dataset.roomid
      };
      uni.navigateTo({
        url: "../groupSetting/groupSetting?groupInfo=" + JSON.stringify(nameList)
      });
    }
  }
};
</script>
<style>
@import "./groups.css";
</style>